﻿using System;
using System.Xml.Serialization;

namespace ClassLibrary
{
    [Serializable]
    public class Event
    {
        #region PRIVATE

        private DateTime _dateBegin;
        private DateTime _dateEnd;
        private string _event;
        private string _link;
        private Guid _guid;
        private string _dateSep = "/";

        #endregion

        [XmlIgnore]
        public Uri link
        {
            get
            {
                Uri res;
                Uri.TryCreate(_link, UriKind.Absolute, out res);
                return res;
            }
        }

        public string linkAsString
        {
            get { return _link; }
            set { _link = value; }
        }
        public bool isPeriod
        {
            get
            {
                if (_dateEnd.Year == 1)
                    return false;
                else
                    return true;
            }
        }
        public Guid ID
        {
            get { return _guid; }
            set { _guid = value; }
        }
        public string title
        {
            get { return _event; }
            set { _event = value; }
        }
        public string dateString
        {
            get
            {
                string day, month;
                if (_dateBegin.Day < 10) day = "0" + _dateBegin.Day;
                else day = _dateBegin.Day.ToString();
                if (_dateBegin.Month < 10) month = "0" + _dateBegin.Month;
                else month = _dateBegin.Month.ToString();

                string date = "(" + day + _dateSep + month + _dateSep + _dateBegin.Year;

                if (_dateEnd.Year != 1)//Datefin.year == 1 if no end date defined
                {
                    if (_dateEnd.Day < 10) day = "0" + _dateEnd.Day;
                    else day = _dateEnd.Day.ToString();
                    if (_dateEnd.Month < 10) month = "0" + _dateEnd.Month;
                    else month = _dateEnd.Month.ToString();

                    date += " - " + day + _dateSep + month + _dateSep + _dateEnd.Year;
                }
                date += ")";

                return date;
            }
        }

        public bool isLink
        {
            get
            {
                Uri temp;
                return Uri.TryCreate(_link, UriKind.Absolute, out temp);
            }

        }
        public DateTime dateBegin
        {
            get { return _dateBegin; }
            set { _dateBegin = value; }
        }
        public DateTime dateEnd
        {
            get { return _dateEnd; }
            set { _dateEnd = value; }
        }

        public void set(Guid guid, string titre, string lien, DateTime d1, DateTime d2)
        {
            _dateBegin = d1;
            _dateEnd = d2;
            _event = titre;
            _guid = guid;
            _link = lien;
        }
        public void set(string titre, string lien, string date)
        {
            _guid = Guid.NewGuid();
            _event = titre;
            _link = lien;

            string separator = " - ";
            int idx = date.IndexOf(separator);
            if (-1 != idx)
            {
                string d1 = date.Substring(0, idx);
                DateTime.TryParse(d1, out _dateBegin);
                string d2 = date.Substring(idx + separator.Length);
                DateTime.TryParse(d2, out _dateEnd);
            }
            else
            {
                DateTime.TryParse(date, out _dateBegin);
                _dateEnd = new DateTime();
            }
        }

        public Event _Clone()
        {
            Event res = new Event();
            res._dateBegin = _dateBegin;
            res._dateEnd = _dateEnd;
            res._event = _event;
            res._link = _link;
            res._guid = _guid;
            res._dateSep = _dateSep;

            return res;
        }
    }
}
